package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.base.BaseObjectListAdapter;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.chat.ChatActivity;
import com.ykdl.member.kid.chat.CommunicateAcivity;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.views.CircularImage;
/********************************************************************************

** 作者： jiafeiyu
** 创始时间：2015-4-28
** 描述：
**    门店货架--之优惠券adapter
 * @param <E>

*********************************************************************************/
public class CouponFragmentAdapter extends BaseAdapter {
	private List<TicketBean>  tickets;
	private BitmapUtils bitmapUtil ;
	private LayoutInflater inflater;
	private onSelectListener mOnSelectListener;
	private List<Integer> mCheckedList;
	private Context mContext;

	public void setData(List<TicketBean>  tickets){
		this.tickets = tickets;
	}
	/**
	 * 用于记录是否已经添加了
	 * @param mCheckedList
	 */
	public void setCheckedList(List<Integer> mCheckedList){
		this.mCheckedList = mCheckedList;
	}
	
	
	public CouponFragmentAdapter(Context context,onSelectListener mOnSelectListener) {
		mContext=context;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defult_head_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_head_icon);
		
		inflater = LayoutInflater.from(context);
		// TODO Auto-generated constructor stub
		this.mOnSelectListener = mOnSelectListener;
	}

	@Override
	public int getCount() {
		if (tickets!=null&&tickets.size()>0){
			return tickets.size();
		}else {
			return 0;
		}
			
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView==null) {
			convertView = inflater.inflate(R.layout.item_coupon_fragment_adapter, null);
			holder = new ViewHolder();
			holder.iv_coupon_fragment_back_logo  = (CircularImage) convertView.findViewById(R.id.iv_coupon_fragment_back_logo);
			holder.iv_coupon_fragment_vip = (ImageView) convertView.findViewById(R.id.iv_coupon_fragment_vip);
			holder.tv_coupon_fragment_xilie = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_xilie);
			holder.ll_item_coupon_diyong = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_diyong);
			holder.tv_coupon_fragment_diyong_number = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_diyong_number);
			holder.ll_item_coupon_manzeng = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_manzeng);
			holder.tv_coupon_fragment_manzeng_content = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_manzeng_content);
			holder.ll_item_coupon_zhijiang = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_zhijiang);
			holder.tv_coupon_fragment_zhijiang_number =  (TextView) convertView.findViewById(R.id.tv_coupon_fragment_zhijiang_number);
			holder.ll_item_coupon_manjian = (LinearLayout) convertView.findViewById(R.id.ll_item_coupon_manjian);
			holder.tv_coupon_fragment_manjian_content = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_manjian_content);
			holder.tv_coupon_fragment_manjian_number = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_manjian_number);
		//	holder.tv_coupon_fragment__number = (TextView) convertView.findViewById(R.id.tv_coupon_fragment__number);
			holder.bt_coupon_fragment_get_coupon = (Button) convertView.findViewById(R.id.bt_coupon_fragment_get_coupon);
			holder.tv_coupon_fragment_is_get = (TextView) convertView.findViewById(R.id.tv_coupon_fragment_is_get);
			holder.ll_coupon_whole_back = (LinearLayout) convertView.findViewById(R.id.ll_coupon_whole_back);
			holder.rl_item_coupon_detail = (RelativeLayout) convertView.findViewById(R.id.rl_item_coupon_detail);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		initView(holder,position);
		
		return convertView;
	}
	//优惠劵类型:1  # 抵用  2  # 满减  3  # 买赠  4  # 直降
	 private void initView(ViewHolder holder, final int position) {
		 if (tickets==null||tickets.size()==0) 
			return ;
		 TicketBean bean = tickets.get(position);
		 switch (bean.getCoupon_type()) {
		case 1:
			//--green
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_green_back);
			holder.ll_item_coupon_diyong.setVisibility(View.VISIBLE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_coupon_manjian.setVisibility(View.GONE);
			holder.tv_coupon_fragment_diyong_number.setText(bean.getCoupon_num_1());
			
			
			break;
		case 2:
			//--red	
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_red_back);
			holder.ll_item_coupon_diyong.setVisibility(View.GONE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_coupon_manjian.setVisibility(View.VISIBLE);
			
			holder.tv_coupon_fragment_manjian_content.setText("满"+bean.getCoupon_num_1()+"减");
			holder.tv_coupon_fragment_manjian_number.setText(bean.getCoupon_num_2());
			
			
			
			break;
		case 3:
			//--yellow
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_yellow_back);
			holder.ll_item_coupon_diyong.setVisibility(View.GONE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.GONE);
			holder.ll_item_coupon_manjian.setVisibility(View.VISIBLE);
			
			holder.tv_coupon_fragment_manjian_content.setText("买"+bean.getCoupon_num_1()+"赠");
			holder.tv_coupon_fragment_manjian_number.setText(bean.getCoupon_num_2());
//			holder.tv_coupon_fragment_manzeng_content.setText("买"+bean.getCoupon_num_1()+"赠"+bean.getCoupon_num_2());
			
			
			break;
		case 4:
			//--blue
			
			holder.ll_coupon_whole_back.setBackgroundResource(R.drawable.coupon_blue_back);
			holder.ll_item_coupon_diyong.setVisibility(View.GONE);
			holder.ll_item_coupon_manzeng.setVisibility(View.GONE);
			holder.ll_item_coupon_zhijiang.setVisibility(View.VISIBLE);
			holder.ll_item_coupon_manjian.setVisibility(View.GONE);
			
			
			holder.tv_coupon_fragment_zhijiang_number.setText(bean.getCoupon_num_1());

			break;
		default:
			break;
		}
		
		 String url = bean.getLogo().getFile_url();
		 if (!TextUtils.isEmpty(url)) {
			 bitmapUtil.display(holder.iv_coupon_fragment_back_logo, url);
		 }
		 if (bean.isMembers_only()) {
			 holder.iv_coupon_fragment_vip.setVisibility(View.VISIBLE); 
			 
		 }else {
			 holder.iv_coupon_fragment_vip.setVisibility(View.GONE);
		 }
		
		
		 
		 
		 if (!bean.isReceive_flag()) {
			 holder.bt_coupon_fragment_get_coupon.setBackgroundResource(R.drawable.get_coupon_button);		 
			 if (bean.isMembers_only()) {			
				 if (bean.isMember_flag()) {
					 holder.tv_coupon_fragment_is_get.setText("立刻领取");
				 }else {
					 holder.tv_coupon_fragment_is_get.setText("加入领取");
				 }
				 
			 }else {
				 holder.tv_coupon_fragment_is_get.setText("立刻领取");
			}
			
		}else {
			holder.bt_coupon_fragment_get_coupon.setBackgroundResource(R.drawable.geted_coupon_button);
			holder.tv_coupon_fragment_is_get.setText("已领取");
		}
		 if (mCheckedList!=null&&mCheckedList.size()>0) {
				if (mCheckedList.contains(position)) {
					holder.bt_coupon_fragment_get_coupon.setBackgroundResource(R.drawable.geted_coupon_button);
					holder.tv_coupon_fragment_is_get.setText("已领取");
				}
			}
		 holder.tv_coupon_fragment_is_get.getPaint().setFakeBoldText(true); 
		 if (!TextUtils.isEmpty(bean.getDiscount_to())) {
			holder.tv_coupon_fragment_xilie.setText(bean.getDiscount_to());
		 }
		 holder.bt_coupon_fragment_get_coupon.setTag(R.id.account_wheel_time, holder);
		 holder.bt_coupon_fragment_get_coupon.setTag(R.id.accountinfor_wheel_time, bean);
		 holder.bt_coupon_fragment_get_coupon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ViewHolder holder =	(ViewHolder) v.getTag(R.id.account_wheel_time);
				TicketBean bean = (TicketBean) v.getTag(R.id.accountinfor_wheel_time);
				mOnSelectListener.onSelectListener(holder,bean,position);
			}
		});
		 holder.rl_item_coupon_detail.setTag(bean);
		 holder.rl_item_coupon_detail.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 进行查询优惠券详情的跳转
				TicketBean info = (TicketBean) v.getTag();
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.COUPON_ID, info.getCoupon_id());
				intent.putExtra(KidAction.URL_TYPE, 3);
				mContext.startActivity(intent);
			}
		});
		 
		 
	}

	 public class ViewHolder{
		 CircularImage iv_coupon_fragment_back_logo;
		ImageView iv_coupon_fragment_vip;
		TextView tv_coupon_fragment_xilie;
		//设置立即领取和已领取
		TextView tv_coupon_fragment_is_get;
		//抵用
		LinearLayout ll_item_coupon_diyong;
		TextView tv_coupon_fragment_diyong_number;
		//满赠
		LinearLayout ll_item_coupon_manzeng;
		TextView tv_coupon_fragment_manzeng_content;
		//直降
		LinearLayout ll_item_coupon_zhijiang;
		TextView tv_coupon_fragment_zhijiang_number;
		//满减
		LinearLayout ll_item_coupon_manjian;
		TextView tv_coupon_fragment_manjian_content;
		TextView tv_coupon_fragment_manjian_number;
		//整体
		LinearLayout ll_coupon_whole_back;
		
		TextView tv_coupon_fragment__number;	
		//点击按钮
		Button bt_coupon_fragment_get_coupon;
		//点击查询详情
		RelativeLayout rl_item_coupon_detail;
		
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 点击选择进行提交或者是加入门店会员的接口
	 * @author jiafeiyu
	 *
	 */
	public interface onSelectListener{
		boolean onSelectListener(ViewHolder holder, TicketBean bean,int position);
	}
	
}
